
#include <iostream>
#include <unistd.h>
#include "RingQueue.hpp"
using namespace std;
void *consumer(void *arg)
{
    RingQueue<int> *rqueue = (RingQueue<int> *)arg;
    while (true)
    {
        int x = 0;
        rqueue->pop(&x);
        std::cout << pthread_self() << " -->获取数据： " << x << std::endl;
        sleep(1);
    }

    return nullptr;
}
void *product(void *arg)
{
    RingQueue<int> *rqueue = (RingQueue<int> *)arg;
    while (true)
    {
        int x = rand();
        rqueue->push(x);
        cout << pthread_self() << "-->输入数据：" << x << endl;
        // sleep(2);
    }
    return nullptr;
}

int main()
{
    // std::vector<int> v(5);
    // v[1]=0;

    srand((uint64_t)time(nullptr) ^ getpid() ^ 0x32457);
    pthread_t tid1, tid2, tid3, tid4;
    RingQueue<int> *rqueue = new RingQueue<int>();
    pthread_create(&tid1, nullptr, consumer, rqueue);
    pthread_create(&tid2, nullptr, product, rqueue);
    pthread_create(&tid3, nullptr, consumer, rqueue);
    pthread_create(&tid4, nullptr, product, rqueue);
    pthread_join(tid1, nullptr);
    pthread_join(tid2, nullptr);
    pthread_join(tid3, nullptr);
    pthread_join(tid4, nullptr);
    delete rqueue;
    return 0;
}